/**
 * Copyright 2008-2009. Chongqing Communications Industry Services Co.,Ltd Information Technology Branch. All rights
 * reserved. <a>http://www.cqcis.com</a>
 */
package com.cqcis.uip.base.framework.utils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.concurrent.ThreadPoolExecutor;

import com.cqcis.uip.base.logadaptor.LogConfiguration;

/**
 * 日志服务配置工具
 * 
 * @author huangmy
 * @date   2008-4-11
 */
public class LogConfigurationUtil {
	private final static String LOGCONFIG_KEY = LogConfigurationUtil.class.getName() + "_LOGCONFIGURATION_LOGSERVICETHREADPOOLEXECUTOR_KEY";
	
    private LogConfigurationUtil() {    	
    }
    
    /**
     * 取出适配于当前日志配置的所有线程池
     * 
     * @param logConfiguration
     * @return
     */
    @SuppressWarnings("unchecked")
	public static Collection<ThreadPoolExecutor> getTPEsFor(LogConfiguration logConfiguration) {
    	synchronized (logConfiguration) {
    		return (List<ThreadPoolExecutor>)logConfiguration.getAttributes().getAttribute(LOGCONFIG_KEY);    		
    	}
    }
    
    /**
	 * 保存线程池到日志配置中
	 * 
	 * @param logConfiguration
	 * @param tpe
	 */
    @SuppressWarnings("unchecked")
	public static void registerTPEFor(LogConfiguration logConfiguration, ThreadPoolExecutor tpe) {
		synchronized (logConfiguration) {
			List<ThreadPoolExecutor> tpes = (List<ThreadPoolExecutor>)logConfiguration.getAttributes().getAttribute(LOGCONFIG_KEY);
			if (tpes == null) {
				tpes = new ArrayList<ThreadPoolExecutor>();
				logConfiguration.getAttributes().setAttribute(LOGCONFIG_KEY, tpes);			
			}
			
			tpes.add(tpe);
		}		
	}
}
